package com.snail.billing.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.os.Page;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountArray;
import com.snail.billing.data.AccountManager;
import com.snail.billing.data.WorkDataManager;

public class LoginFragment extends Page implements View.OnClickListener, AdapterView.OnItemClickListener {

	private TextView textTitle;
	private View buttonBack;

	private View inputLayout;
	private EditText inputAccount;
	private EditText inputPwd;
	private View button;
	private View forgetButton;
	private View popupButton;

	private boolean isCreate;
	private PopupWindow popupWindow;
	private int pwidth;
	private ListView listView;
	private List<Map<String, String>> data;
	private SimpleAdapter adapter;
	private AccountArray accountArray;
	private Account account;

	private String accountString, pwdString;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_login_activity);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_login_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		inputLayout = findViewById(ResUtil.getViewId(Resource.id.snailbilling_login_input_layout));
		popupButton = findViewById(ResUtil.getViewId(Resource.id.snailbilling_login_account_button));
		popupButton.setOnClickListener(this);
		inputAccount = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_login_input_account));
		inputAccount.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				inputPwd.setText("");
			}
		});
		inputPwd = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_login_input_pwd));
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_login_button));
		button.setOnClickListener(this);
		forgetButton = findViewById(ResUtil.getViewId(Resource.id.snailbilling_login_forget_button));
		forgetButton.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		accountArray = AccountManager.getAccountArray();
		if (accountArray.length() != 0) {
			selectAccount(0);
		}
	}

	private void selectAccount(int position) {
		account = accountArray.getAll().get(position);
		inputAccount.setText(account.getAccount());
		inputPwd.setText(account.getPwd());
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(popupButton)) {
			initPopupWindow();
			showPopup();
		} else if (v.equals(forgetButton)) {
			getPageManager().forward(ForgetFragment.class);
		} else if (v.equals(button)) {
			accountString = inputAccount.getText().toString();
			if (TextUtils.isEmpty(accountString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_login_input_account));
				return;
			}
			pwdString = inputPwd.getText().toString();
			if (TextUtils.isEmpty(pwdString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_login_input_pwd));
				return;
			}
			getPageManager().finish();
			if (DataCache.getInstance().importParams.loginCallback != null) {
				WorkDataManager.getInstance().clear();
				DataCache.getInstance().importParams.loginCallback.onLoginCallback(accountString, pwdString);
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		dismissPopup();
		selectAccount(position);
	}

	private void showPopup() {
		if (accountArray.length() != 0) {
			popupWindow.showAsDropDown(inputLayout);
		}
	}

	private void dismissPopup() {
		popupWindow.dismiss();
	}

	private void initDatas() {
		data = new ArrayList<Map<String, String>>();
		for (Account accountJson : accountArray.getAll()) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("account", accountJson.getAccount());
			data.add(map);
		}
	}

	private void initPopupWindow() {
		if (accountArray.length() == 0) {
			return;
		}
		if (isCreate) {
			return;
		}
		isCreate = true;
		initDatas();
		View view = getPageManager().getLayoutInflater().inflate(ResUtil.getLayoutId(Resource.layout.snailbilling_login_popup_activity), null);
		listView = (ListView) view.findViewById(ResUtil.getViewId(Resource.id.snailbilling_login_popup_listview));
		listView.setOnItemClickListener(this);
		pwidth = inputLayout.getWidth();
		popupWindow = new PopupWindow(view, pwidth, -2, true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new ColorDrawable(0));
		int resource = ResUtil.getLayoutId(Resource.layout.snailbilling_login_popup_item);
		String[] from = { "account" };
		int[] to = { ResUtil.getViewId(Resource.id.snailbilling_login_popup_text) };
		adapter = new SimpleAdapter(getContext(), data, resource, from, to);
		listView.setAdapter(adapter);
	}

}
